/**
 * Created by lee_mxue on 17/2/9.
 */
import React from "react";
import ReactDOM from "react-dom";
import AJAX from "../../lib/ajax";
import { addErrorListener } from "../../lib/handleError"
import weixinShare from "../../lib/weixinShare";
import Tools from "../../lib/tools";

require('../../lib/rem');
require('./SendingPackageReceiverAddress.less');
require('../../styles/common.less');
require('../../styles/lux.less')
//判断寄件类型（1: 服务站点寄件    2: 柜体寄件）
const sendType = Tools.getParams().send_type;
class App extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            isReady: false,     //是否加载页面
            addressData: {
                count: 0,
                list: []
            }
        };

        this.SelectReceiverAddress = this.SelectReceiverAddress.bind(this);   //获取收件人地址
        this.handleEditAddress = this.handleEditAddress.bind(this);   //编辑收件地址

    }

    componentWillMount() {
        addErrorListener()()
        weixinShare.hideAll();

        AJAX.request("address_list", {
            type: 2,
            page: 1,
            psize: 10
        }, (data) => {
            if (data.code == 0) {
                let myAddressData = {
                    count: data.body.count,
                    list: data.body.list
                }
                this.setState({
                    isReady: true,     //是否加载页面
                    addressData: myAddressData
                });
            }
            // console.log("data: ", data)
        })
    }

    componentDidMount() {
        AJAX.appendBaiduAnalysis()
    }

    //选择收件人地址
    SelectReceiverAddress(data) {
        //将收件人地址信息存入localStorage
        var storage = window.localStorage;

        //判断寄件类型（1: 服务站点寄件    2: 柜体寄件）
        //服务站点寄件
        if (sendType == 1) {
            storage.receiverData_x = JSON.stringify(data)
            window.location.href = "./SendingPackageHeader";
            // console.log(JSON.parse(storage.receiverData));
        } else if (sendType == 2) {
            //柜体寄件
            storage.cabinet_receiverData_x = JSON.stringify(data)
            window.location.href = "./CabinetSendingHeader";
            // console.log(JSON.parse(storage.receiverData));

        }

    }

    //编辑收件地址
    handleEditAddress(data) {
        var storage = window.localStorage;
        storage.receiverData_x = JSON.stringify(data)
        //判断寄件类型（1: 服务站点寄件    2: 柜体寄件）
        window.location.href = "./SendingPackageEditReceiverAddress?send_type="+sendType;

    }

    render() {
        return (
            this.state.isReady &&
            <div className="main">
                <div className="center_content addressList">
                    {
                        this.state.addressData.list.map(item => {
                            return (
                                <div className="addressItem">
                                    <div onClick={this.SelectReceiverAddress.bind(this, item)}>
                                        <p>
                                            <span>{item.realname}</span>
                                            <span className="textR">{item.phone}</span>

                                        </p>
                                        <p>
                                            <span className="textBlue">[{item.tag}]</span>
                                            <span>{item.province} {item.city} {item.county} {item.address}</span>
                                        </p>
                                    </div>
                                    <i className="iconfont iconB icon-bianji"
                                       onClick={this.handleEditAddress.bind(this, item)}></i>
                                </div>                            )
                        })
                    }

                </div>
                <div className="bottom_submit">
                    <a className="weui-btn weui-btn_primary"
                       href={"./SendingPackageAddReceiverAddress?send_type=" + sendType}
                       id="showTooltips">+ 添加收件人</a>
                </div>
            </div>

        )
    }
}
ReactDOM.render(<App />, document.getElementById('app'));
if (module.hot) {
    module.hot.accept();
}